#include "raylib.h"

int main(void)
{
    // 初始化窗口
    const int screenWidth = 2000;
    const int screenHeight = 1500;
    InitWindow(screenWidth, screenHeight, "raylib");

    // 加载纹理
    Texture2D texture = LoadTexture("assets/imgs/reisen.png");
    
    // 加载像素化着色器
    Shader shader = LoadShader(0, "assets/shaders/pixel.fs");

    // 获取着色器统一变量位置
    int textureSizeLoc = GetShaderLocation(shader, "textureSize");
    int pixelSizeLoc = GetShaderLocation(shader, "pixelSize");

    // 设置着色器参数
    float textureSize[2] = { (float)texture.width, (float)texture.height };
    float pixelSize = 4.0f;
    
    SetShaderValue(shader, textureSizeLoc, textureSize, SHADER_UNIFORM_VEC2);
    SetShaderValue(shader, pixelSizeLoc, &pixelSize, SHADER_UNIFORM_FLOAT);

    // 主游戏循环
    SetTargetFPS(60);

    while (!WindowShouldClose())
    {
        // 更新像素大小（示例：通过按键改变）
        if (IsKeyPressed(KEY_UP)) pixelSize += 1.0f;
        if (IsKeyPressed(KEY_DOWN) && pixelSize > 1.0f) pixelSize -= 1.0f;
        SetShaderValue(shader, pixelSizeLoc, &pixelSize, SHADER_UNIFORM_FLOAT);

        // 绘制
        BeginDrawing();
            ClearBackground(RAYWHITE);
            
            // 原始纹理
            DrawTexture(texture, 50, 150, WHITE);
            DrawText("原始", 50, 120, 20, DARKGRAY);
            
            // 使用着色器的纹理
            BeginShaderMode(shader);
                DrawTexture(texture, 400, 150, WHITE);
            EndShaderMode();
            DrawText("像素化", 400, 120, 20, DARKGRAY);
            
            DrawText("像素化着色器示例", 10, 10, 30, DARKGRAY);
            DrawText("使用 UP/DOWN 键调整像素大小", 10, 50, 20, DARKGRAY);
            DrawText(TextFormat("当前像素大小: %.0f", pixelSize), 10, 80, 20, DARKBLUE);
            DrawFPS(10, 110);
        EndDrawing();
    }

    // 清理资源
    UnloadShader(shader);
    UnloadTexture(texture);
    CloseWindow();

    return 0;
}